#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPainter>
#include <QDebug>
#include <QFileDialog>
#include <QString>
#include <QProgressDialog>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // 1. 初始化界面相关
    connect(ui->action_open, &QAction::triggered, this, &MainWindow::openVideo);
    connect(ui->action_save, &QAction::triggered, this, &MainWindow::saveVideo);

    // 2. 初始化视频播放相关
    m_Widget_PlayVideo = new QWidget_PlayVideo(this);
    this->setCentralWidget(m_Widget_PlayVideo);

    // 3. 初始化视频处理相关
    m_FFmpegProcessing = new FFmpegVideo();
    m_ProcessingThread = new QThread(this);
    m_FFmpegProcessing->moveToThread(m_ProcessingThread);  // 移动到线程中
}

MainWindow::~MainWindow()
{
    if(m_ProcessingThread->isRunning()){
        m_ProcessingThread->quit();
        m_ProcessingThread->wait();
    }
    delete m_ProcessingThread;
    delete m_FFmpegProcessing;
    delete ui;
}

void MainWindow::openVideo()
{
    QString filePath = QFileDialog::getOpenFileName(this, QObject::tr("Open File"),
                                                    nullptr,  // QDir::homePath(),
                                                    QObject::tr("mp4 (*.mp4) ;; All Files (*)"));
    if (!filePath.isEmpty()) {
        // 如果正在运行，需先退出线程
        if(m_Widget_PlayVideo->m_PlayThread->isRunning()){
            m_Widget_PlayVideo->m_FFmpegVideo->m_stopPlay = true;
            m_Widget_PlayVideo->m_PlayThread->quit();
            m_Widget_PlayVideo->m_PlayThread->wait();
        }

        // 读取视频
        m_Widget_PlayVideo->m_FFmpegVideo->loadVideoFile(filePath);
        m_Widget_PlayVideo->m_slider->setRange(0, static_cast<int>(m_Widget_PlayVideo->m_FFmpegVideo->m_frame_num));
        m_Widget_PlayVideo->m_PlayThread->start();
        m_Widget_PlayVideo->m_PlayThread->quit();   // 执行完后自动关闭，否则一直在运行中... 无法重新start发送开始信号
    }
}


void MainWindow::saveVideo()
{
//    if(!m_FFmpegVideo){
//        return;
//    }
    m_FFmpegProcessing->loadVideoFile(m_Widget_PlayVideo->m_FFmpegVideo->m_filename);  // 读取视频
    QFileInfo fileInfo(m_FFmpegProcessing->m_filename);
    QString filePath = QFileDialog::getSaveFileName(this, QObject::tr("Open File"),
                                                    fileInfo.completeBaseName() + ".gif",
                                                    QObject::tr("gif (*.gif) ;; All Files (*)"));

    if (filePath.isEmpty()) {
        return;
    }
    m_FFmpegProcessing->m_outfilename = filePath; // 输出文件
    fileInfo.setFile(filePath);

    // 转GIF ------------
    int ret = fileInfo.suffix().compare(QString("gif"), Qt::CaseInsensitive);
    // 进度条
    progressDialog = new QProgressDialog();
    progressDialog->setMinimumWidth(300);               // 设置最小宽度
    progressDialog->setWindowModality(Qt::NonModal);    // 非模态，其它窗口正常交互  Qt::WindowModal 模态
    progressDialog->setMinimumDuration(0);              // 等待0秒后显示
    progressDialog->setWindowTitle(tr("进度条框"));      // 标题名
    progressDialog->setLabelText(tr("正在转换"));        // 标签的
    progressDialog->setCancelButtonText(tr("放弃"));    // 取消按钮
    progressDialog->setRange(0, static_cast<int>(m_FFmpegProcessing->m_frame_num));    // 考虑是否移换种方式显示进度条进度... 不使用帧数

    // 转换
    if(ret == 0){
        // 进度条绑定
        connect(m_FFmpegProcessing, &FFmpegVideo::sig_SendFrameNum, this, [&](int num){
            if(progressDialog->wasCanceled()){    // 弹窗的取消按钮
                m_FFmpegProcessing->m_stopProcessing = true;
                return;
            }
            progressDialog->setValue(num);
        }, Qt::BlockingQueuedConnection);  // 发送信号后，先执行此内容 再继续执行线程，保证线程可以及时推出


        // 开始转换  在这里连接需注意Qt::UniqueConnection 使得连接唯一
        connect(m_ProcessingThread, SIGNAL(started()), m_FFmpegProcessing, SLOT(savetoGif()), Qt::UniqueConnection);
        connect(m_ProcessingThread, &QThread::finished, progressDialog, &QProgressDialog::deleteLater, Qt::UniqueConnection);
        m_ProcessingThread->start();
        m_ProcessingThread->quit();
    }
}
